package com.daocao.hua.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.bumptech.glide.Glide;
import com.daocao.hua.R;
import com.daocao.hua.adapter.ViewPagerAdapter;
import com.daocao.hua.util.PreferenceUtils;
import com.daocao.hua.http.VolleySingleton;
import com.daocao.hua.model.GuidePageModel;
import com.daocao.hua.util.ImageUtil;
import com.daocao.hua.util.Util;
import com.daocao.hua.view.V19FrameLayout;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

/**
 * 引导页
 * Created by Master.Dong on 16/4/10.
 */
public class GuideActivity extends Activity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    // 定义ViewPager对象
    private ViewPager viewPager;
    // 定义ViewPager适配器
    private ViewPagerAdapter vpAdapter;
    // 定义一个ArrayList来存放View
    private ArrayList<View> views;
    // 引导图片资源
//    private static final int[] pics = {R.mipmap.start_1, R.mipmap.start_2,
//            R.mipmap.start_3, R.mipmap.start_4};
    // 底部小点的图片


    //引导页list
    private List<GuidePageModel.GuideData> list;

    private int currentPage;

    private ImageView btn;
    private TextView jump;

    private RelativeLayout view;

    private GuidePageModel model;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //不显示程序的标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //不显示系统的标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.guide_layout);

        try {
            model = (GuidePageModel) getIntent().getSerializableExtra("GuidePage");
            if (model == null) {
                startActivity();
                return;
            }
            list = model.getData();

            initView();
            initData();
        } catch (Exception e) {
            e.printStackTrace();
            startActivity();
        }

    }

    /**
     * 初始化组件
     */
    private void initView() {

        // 实例化ArrayList对象
        views = new ArrayList<View>();
        // 实例化ViewPager
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        // 实例化ViewPager适配器
        vpAdapter = new ViewPagerAdapter(getApplicationContext(), views);

        view = (RelativeLayout) findViewById(R.id.view);
        btn = (ImageView) findViewById(R.id.btn);
        jump = (TextView) findViewById(R.id.jump);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity();
            }
        });
        jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity();
            }
        });

    }

    private void startActivity() {
        try {
            PreferenceUtils.putBoolean(GuideActivity.this, "startFirst", true);
            if (TextUtils.isEmpty(model.getUpdate_time()))
                PreferenceUtils.putLong(GuideActivity.this, "update_time", System.currentTimeMillis());
            else
                PreferenceUtils.putLong(GuideActivity.this, "update_time", Long.parseLong(Util.getTime(model.getUpdate_time())));
            Util.startActivity(GuideActivity.this, MainActivityNew.class);
            GuideActivity.this.finish();
            overridePendingTransition(R.anim.splash_in, R.anim.splash_out);

        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化数据
     */
    private void initData() {
        // 定义一个布局并设置参数
        Glide.with(this).load(model.button_bg).error(R.mipmap.img_repay_left_btn).into(btn);
        setImage(list);


    }


    private void setImage(List<GuidePageModel.GuideData> list) {

        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {

                final V19FrameLayout frameLayout = (V19FrameLayout) getLayoutInflater().inflate(R.layout.guide_image, null);
                ImageView iv = (ImageView) frameLayout.findViewById(R.id.image_guide);

                VolleySingleton.getRequestQueue().add(new ImageRequest(list.get(i).getIcon(),
                        new Response.Listener<Bitmap>() {
                            @Override
                            public void onResponse(Bitmap bitmap) {
                                //加载图片资源
                                iv.setImageBitmap(bitmap);
                            }
                        }, 0, 0, Bitmap.Config.RGB_565,
                        new Response.ErrorListener() {
                            public void onErrorResponse(VolleyError error) {

                            }
                        }));
                views.add(frameLayout);

            }

            // 设置数据
            viewPager.setAdapter(vpAdapter);
            // 设置监听
            viewPager.addOnPageChangeListener(this);

            // 初始化底部小点

        } else {
            startActivity();
        }

    }


    /**
     * 滑动状态改变时调用
     */
    @Override
    public void onPageScrollStateChanged(int arg0) {
//        ToastUtil.makeText(this,arg0+"  --", Toast.LENGTH_LONG).show();
    }

    /**
     * 当前页面滑动时调用
     */
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        currentPage = arg0;
        view.setBackgroundColor(Color.parseColor(model.getData().get(arg0).getBackground_color()));
        if (arg0 == list.size() - 1) {
            btn.setVisibility(View.VISIBLE);
//            viewPager.setOnTouchListener(new mTouchListener());
        } else {
            btn.setVisibility(View.GONE);
        }
    }

    /**
     * 新的页面被选中时调用
     */
    @Override
    public void onPageSelected(int arg0) {
        // 设置底部小点选中状态
    }

    @Override
    public void onClick(View v) {
    }


//    class mTouchListener implements View.OnTouchListener {
//
//        float x1 = 0;
//        float x2 = 0;
//
//        @Override
//        public boolean onTouch(View v, MotionEvent event) {
//            if (currentPage == list.size() - 1) {
//                switch (event.getAction()) {
//                    case MotionEvent.ACTION_DOWN:
//                        //当手指按下的时候
//                        x1 = event.getX();
//                        break;
//                    case MotionEvent.ACTION_UP:
//                        //当手指离开的时候
//                        x2 = event.getX();
//                        if (x2 - x1 > 50) {
//                            startActivity();
//                        }
//                        break;
//                }
//            }
//            return false;
//        }
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            views = null;
            viewPager = null;
//            ImageUtil.releaseImageViewResouce(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("GuideActivity");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("GuideActivity");
        MobclickAgent.onPause(this);
    }

}
